tmap

Lire un shape

Commençons par importer un fichier shapefile :

library(tmap)

departements <- read_shape("./data/departements/DEPARTEMENT.shp")

On vérifie la projection utilisée :

get_projection(departements)
## [1] "+proj=lcc +lat_1=44 +lat_2=49 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m +no_defs"
bb(departements)
##       min     max
## x   99226 1242375
## y 6049647 7110480

On la modifie :

# pour quelques projections possibles, voir ?get_proj4

departements <- set_projection(departements, projection = "longlat") # coordonnées géographiques
bb(departements)
##         min       max
## x -5.139017  9.559823
## y 41.362757 51.089000

Représentation carte sans données

tm_shape(departements) +
  tm_borders()

Utiliser un fonds de carte OSM

À noter que par défaut read_osm télécharge un raster (une image), mais qu’on peut aussi lui demander des données vectorielles (voir le manuel : ?read_osm).

fonds <- read_osm(bb(departements, ext = 1.2), minNumTiles = 10, type = "stamen-watercolor")

tm_shape(fonds) +
  tm_raster() +
tm_shape(departements) +
  tm_borders(col = "white")

Créer une carte choroplèthe

On peut pour chaque couche (layer) choisir une esthétique. Encore faut-il avoir des données à représenter.

load("./data/Pres2012dpts.Rdata")

departements <- append_data(departements, dpts, key.shp = "CODE_DEPT", key.data = "CodeDpt")
## Keys match perfectly.
tm_shape(fonds) +
  tm_raster() +
tm_shape(departements) +
  tm_borders(col = "white") +
  tm_fill(col = "Sarkozy2.exp", alpha = 0.8, style = "quantile", n = 6, palette = "OrRd", legend.format = list(text.separator = "à", digits = 1), title = "Sarkozy 2e tour") +
tm_layout(legend.position = c("left", "bottom")) +
  tm_scale_bar(position = c("center", "bottom")) +
  tm_compass()

Créer une carte avec cercles proportionnels

tm_shape(fonds) +
  tm_raster() +
tm_shape(departements) +
  tm_borders(col = "white") +
  tm_bubbles(size = "Sarkozy2", 
             col = "Sarkozy2.exp", 
             alpha = 0.8, 
             style = "quantile", 
             n = 6, 
             palette = "Reds", 
             legend.format = list(text.separator = "à", digits = 1), 
             title.col = "Sarkozy 2e tour", 
             title.size = "Voix pour Sarkozy", 
             legend.size.is.portrait = TRUE) +
tm_layout(legend.position = c("left", "bottom"))

Petits multiples

tm_shape(departements) +
  tm_borders(col = "white") +
  tm_fill(col = c("Hollande2.exp", "Sarkozy2.exp"), alpha = 0.8, style = "quantile", n = 6, palette = "Reds", legend.format = list(text.separator = "à", digits = 1), title = c("Hollande 2e tour", "Sarkozy 2e tour")) +
tm_layout(legend.position = c("left", "bottom"))

On peut également utiliser tm_facets si le dataframe est en mode long (une variable permet de dispatcher les cartes).

Cartographie interactive/web

tm_shape(departements) +
  tm_borders(col = "white") +
  tm_fill(col = "Sarkozy2.exp", alpha = 0.8, style = "quantile", n = 6, palette = "Reds", legend.format = list(text.separator = "à", digits = 1), title = "Sarkozy 2e tour") +
tm_layout(legend.position = c("left", "bottom"))
ttm()

tm_shape(departements) +
  tm_borders(col = "white") +
  tm_fill(col = "Sarkozy2.exp", alpha = 0.8, style = "quantile", n = 6, palette = "Reds", legend.format = list(text.separator = "à", digits = 1), title = "Sarkozy 2e tour") +
tm_layout(legend.position = c("left", "bottom"))

leaflet et mapview

Pour de la cartographie orientée interactive/web, leaflet est le package le plus adapté.

Il fonctionne de manière simple, avec des pipes.

library(leaflet)

leaflet(data = departements) %>% 
  addTiles() %>% 
  addPolygons(fillColor = ~colorQuantile("Blues", departements@data$Sarkozy2.exp, n = 6)(Sarkozy2.exp), color = "transparent", fillOpacity = 0.6)

On peut ajouter des popups :

library(htmltools)

leaflet(data = departements) %>% 
  addTiles() %>% 
  addPolygons(fillColor = ~colorQuantile("Blues", departements@data$Sarkozy2.exp, n = 6)(Sarkozy2.exp), 
              color = "transparent", 
              fillOpacity = 0.6, 
              popup = ~paste0("<em>Sarkozy (2e tour)</em> : ", round(Sarkozy2.exp, 1))) 

mapview permet d’explorer rapidement un shape.

library(mapview)

mapview(departements)